package com.ruoyi.web.controller.jiaxiao;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.JxExamRecord;
import com.ruoyi.system.service.IJxExamRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学员考试记录Controller
 * 
 * @author ruoyi
 * @date 2025-01-27
 */
@RestController
@RequestMapping("/jiaxiao/exam")
public class JxExamRecordController extends BaseController
{
    @Autowired
    private IJxExamRecordService jxExamRecordService;

    /**
     * 查询学员考试记录列表
     */
    @PreAuthorize("@ss.hasPermi('jiaxiao:exam:list')")
    @GetMapping("/list")
    public TableDataInfo list(JxExamRecord jxExamRecord)
    {
        startPage();
        List<JxExamRecord> list = jxExamRecordService.selectJxExamRecordList(jxExamRecord);
        return getDataTable(list);
    }

    /**
     * 导出学员考试记录列表
     */
    @PreAuthorize("@ss.hasPermi('jiaxiao:exam:export')")
    @Log(title = "学员考试记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JxExamRecord jxExamRecord)
    {
        List<JxExamRecord> list = jxExamRecordService.selectJxExamRecordList(jxExamRecord);
        ExcelUtil<JxExamRecord> util = new ExcelUtil<JxExamRecord>(JxExamRecord.class);
        util.exportExcel(response, list, "学员考试记录数据");
    }

    /**
     * 获取学员考试记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('jiaxiao:exam:query')")
    @GetMapping(value = "/{examId}")
    public AjaxResult getInfo(@PathVariable("examId") Long examId)
    {
        return success(jxExamRecordService.selectJxExamRecordByExamId(examId));
    }

    /**
     * 新增学员考试记录
     */
    @PreAuthorize("@ss.hasPermi('jiaxiao:exam:add')")
    @Log(title = "学员考试记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JxExamRecord jxExamRecord)
    {
        return toAjax(jxExamRecordService.insertJxExamRecord(jxExamRecord));
    }

    /**
     * 登记考试记录（包含业务规则）
     */
    @PreAuthorize("@ss.hasPermi('jiaxiao:exam:register')")
    @Log(title = "登记考试记录", businessType = BusinessType.INSERT)
    @PostMapping("/register")
    public AjaxResult register(@RequestBody JxExamRecord jxExamRecord)
    {
        try {
            return toAjax(jxExamRecordService.registerExamRecord(jxExamRecord));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改学员考试记录
     */
    @PreAuthorize("@ss.hasPermi('jiaxiao:exam:edit')")
    @Log(title = "学员考试记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JxExamRecord jxExamRecord)
    {
        return toAjax(jxExamRecordService.updateJxExamRecord(jxExamRecord));
    }

    /**
     * 删除学员考试记录
     */
    @PreAuthorize("@ss.hasPermi('jiaxiao:exam:remove')")
    @Log(title = "学员考试记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{examIds}")
    public AjaxResult remove(@PathVariable Long[] examIds)
    {
        return toAjax(jxExamRecordService.deleteJxExamRecordByExamIds(examIds));
    }

    /**
     * 查询学员某科目的考试记录
     */
    @PreAuthorize("@ss.hasPermi('jiaxiao:exam:query')")
    @GetMapping("/student/{studentId}/subject/{subjectId}")
    public AjaxResult getExamRecordsByStudentAndSubject(@PathVariable("studentId") Long studentId, 
                                                       @PathVariable("subjectId") Long subjectId)
    {
        List<JxExamRecord> list = jxExamRecordService.selectJxExamRecordByStudentAndSubject(studentId, subjectId);
        return success(list);
    }

    /**
     * 查询学员所有科目的考试记录
     */
    @PreAuthorize("@ss.hasPermi('jiaxiao:exam:query')")
    @GetMapping("/student/{studentId}")
    public AjaxResult getExamRecordsByStudent(@PathVariable("studentId") Long studentId)
    {
        List<JxExamRecord> list = jxExamRecordService.selectJxExamRecordByStudent(studentId);
        return success(list);
    }

    /**
     * 检查学员是否可以参加某科目考试
     */
    @PreAuthorize("@ss.hasPermi('jiaxiao:exam:query')")
    @GetMapping("/canTake/{studentId}/{subjectId}")
    public AjaxResult canTakeExam(@PathVariable("studentId") Long studentId, 
                                 @PathVariable("subjectId") Long subjectId)
    {
        boolean canTake = jxExamRecordService.canTakeExam(studentId, subjectId);
        return success(canTake);
    }
}
